package com.hq.ims.data.utils;

import com.alibaba.fastjson2.JSONObject;
import com.hq.utils.util.JsonUtils;

/**
 * jbp
 **/
public class JsonAnalyzeUtil {

    public static String findJsonValue(String jsonStr, String key) {
        JSONObject jsonObject = JsonUtils.stringToObject(jsonStr, JSONObject.class);
        return findJsonValue(jsonObject, key);
    }

    public static String findJsonValue(JSONObject jsonObject, String key) {
        String[] split = key.split("\\.");
        try {
            for (int i = 0; i < split.length; ++i) {
                if (i == split.length - 1) {
                    return jsonObject.getString(split[i]);
                }

                jsonObject = jsonObject.getJSONObject(split[i]);
                if (jsonObject == null) {
                    return "";
                }
            }
        } catch (Exception e) {
            return "";
        }
        return "";
    }
}
